/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mudalov.rec.util.webui;

import com.mudalov.rec.dao.interfaces.IMaterialDAO;
import com.mudalov.rec.dao.interfaces.ISubjectDAO;
import com.mudalov.rec.dao.interfaces.ITestDAO;
import com.mudalov.rec.dao.model.study.Material;
import com.mudalov.rec.dao.model.study.Question;
import com.mudalov.rec.dao.model.study.Subject;
import com.mudalov.rec.dao.model.study.Test;
import com.mudalov.rec.dao.model.user.Role;
import com.mudalov.rec.web.SessionBean1;
import com.sun.webui.jsf.model.Option;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import com.sun.webui.jsf.util.MessageUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;

/**
 *
 * @author Mikhail_Udalov
 */
public class DropDownHelper {

    private SessionBean1 sessionBean;

    public DropDownHelper(SessionBean1 sessionBean) {
        this.sessionBean = sessionBean;
    }

    public void populateSubjectsDropDown(ISubjectDAO dao, SingleSelectOptionsList optionsList) {
        sessionBean.getCurrentSubjects().clear();
        List<Subject> subjectList = dao.getSubjects();
        List<Option> options = new ArrayList<Option>();
        for (Subject subject : subjectList) {
            Option option = new Option(subject.getId().toString(), subject.getText());
            sessionBean.getCurrentSubjects().put(subject.getId().toString(), subject);
            options.add(option);
        }
        optionsList.setOptions(options.toArray(new Option[0]));
        if (options.size() > 0) {
            optionsList.setSelectedValue(options.get(0));
        }
    }


    public Option[] getRoleOptions() {
        FacesContext context = FacesContext.getCurrentInstance();
		String text = MessageUtil.getMessage(context, "com.mudalov.rec.web.Messages", "role_admin");
        Option admin = new Option(Role.Type.ADMIN, text);
        text = MessageUtil.getMessage(context, "com.mudalov.rec.web.Messages", "role_student");
        Option student = new Option(Role.Type.STUDENT, text);
        text = MessageUtil.getMessage(context, "com.mudalov.rec.web.Messages", "role_teacher");
        Option teacher = new Option(Role.Type.TEACHER, text);
        return new Option[] {admin, student, teacher};
    }

    public Option[] getQuestionTypeOptions() {
        FacesContext context = FacesContext.getCurrentInstance();
		String text = MessageUtil.getMessage(context, "com.mudalov.rec.web.Messages", "question_type_multiple");
        Option multiple = new Option(Question.QuestionType.CHECKBOX, text);
        text = MessageUtil.getMessage(context, "com.mudalov.rec.web.Messages", "question_type_exact");
        Option exact = new Option(Question.QuestionType.RADIO, text);
        return new Option[] {multiple, exact};
    }

    public Option[] getTestsOptions(ITestDAO testDAO, Subject subject) {
        String subjectText = subject != null ? subject.getText() : "";
        List<Test> tests = testDAO.getTests("", "", subjectText);
        List<Option> options = new ArrayList<Option>();
        for (Test test : tests) {
            options.add(new Option(test.getName(), test.getName()));
        }
        return options.toArray(new Option[]{});
    }

    public Option[] getMaterialsOptions(IMaterialDAO materialDAO, Subject subject) {
        String subjectText = subject != null ? subject.getText() : "";
        List<Material> materials = materialDAO.getMaterials("", "", subjectText);
        List<Option> options = new ArrayList<Option>();
        for (Material material : materials) {
            options.add(new Option(material.getName(), material.getName()));
        }
        return options.toArray(new Option[]{});
    }

}
